﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autocentro.Entities;
using System.Data.Objects;

namespace Autocentro.Respository
{
    public class MarcaRepository:MasterRepository, IMarcaRepository
    {
        public List<MARCA> GetAllFromMarcas()
        {
            var query = from m in context.Marca
                        select m;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);
            return query.ToList();
        }

        public List<MARCA> GetAFromMarcasByCriterio(string criterio)
        {
            var query = from m in context.Marca
                        select m;
            if (criterio!=null && criterio!="" && criterio!= string.Empty)
            {
                query = from m in query
                        where m.Descripcion.ToUpper().Contains(criterio.ToUpper())
                        select m;
            }

            ObjectContext.Refresh(RefreshMode.StoreWins, query);
            return query.ToList();
        }

        public MARCA GetFromMarcasByID(int id)
        {
            return context.Marca.Find(id);
        }

        public void AddMarca(MARCA marca)
        {
            context.Marca.Add(marca);
            context.SaveChanges();
        }

        public void UpdateMarca(MARCA marca)
        {
            context.Entry(marca).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveMarca(int id)
        {
            var exite = context.Marca.Find(id);
            if (exite!=null)
            {
                context.Marca.Remove(exite);
                context.SaveChanges();
            }
        }

        public bool ExiteDuplicidad(string descripcion, int id)
        {
            var query = from m in context.Marca
                        where m.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0 &&
                        m.Id != id
                        select m;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);
            return query.Any();
        }
    }
}
